import pymysql
import psutil
import time
import socket


#连接到数据库
db = pymysql.connect("192.168.0.163","wangfeng","123456","windows_system_info",charset="utf8")
cursor = db.cursor()
db.autocommit(True)

#采集信息（cpu和内存）
# hostname ip_cpu 磁盘的根分区的使用率

c_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
hostname = socket.gethostname()
ip = psutil.net_if_addrs()["本地连接"][1][1]
cpu_percent = psutil.cpu_percent()
partition_root = psutil.disk_usage("c:\\").percent
print(c_time)


sql = "insert into network_disk_cpu(hostname,ip,disk_root,c_time,cpu_percent) value ('{}','{}',{},'{}',{})".format(hostname,ip,partition_root,c_time,cpu_percent)
cursor.execute(sql)
db.close()